$(function () {
    /**
     * body 最小尺寸
     */
    function min_body_height() {
        var min_height = $(window).height() - 140;
        $('#admin-layout-body .admin-body').css({
            'min-height': min_height + 'px'
        });
        $('#admin-layout-body .admin-body-height').css({
            'height': min_height + 'px'
        });
    }

    window.addEventListener('resize', min_body_height);
    window.addEventListener('load', min_body_height);

    $('body').on('click', '.btn-page-back', function () {
        window.history.go(-1);
    });

    /**
     * toastr 通知提醒配置
     * @type {{closeButton: boolean, debug: boolean, progressBar: boolean, positionClass: string, onclick: null, showDuration: string, hideDuration: string, timeOut: string, extendedTimeOut: string, showEasing: string, hideEasing: string, showMethod: string, hideMethod: string}}
     */
    toastr.options = {
        "positionClass": "toast-top-center"
    };


    // 退出登录
    $('#logout').on('click', function () {
        AjaxGet({
            url:'/auth/logout',
            success:function (res) {
                redirect(res,1000)
            }
        })
    })
});



/**
 * 使用 layerTemplate(params) 代替
 *
 * @param params
 * @param layer
 * @param laytpl
 */
function layer_with_tpl(params, layer, laytpl) {
    laytpl(params.tpl).render(params.datas || {}, function (html) {
        var default_params = {
            type: 1,
            resize: false,
            content: html
        };
        if (params.onReady) default_params.success = params.onReady;
        layer.open(Object.assign(default_params, params.layer));
    });
}

function LoadingButtonObject(sel) {
    var queryDom = $(sel);
    var default_content = queryDom.html();
    var loading_content ='<i class="layui-icon layui-icon layui-anim layui-anim-rotate layui-anim-loop">&#xe63e</i>';


    function start() {
        queryDom.html(loading_content);
        queryDom.attr('disabled', true);
        queryDom.addClass('layui-btn-disabled');
    }

    function stop() {
        queryDom.html(default_content);
        queryDom.attr('disabled', false);
        queryDom.removeClass('layui-btn-disabled');
    }
}

function loadingButton(sel) {
    return new LoadingButtonObject(sel);
}

function redirect(url, delay) {
    if (delay && delay > 0) {
        setTimeout(function () {
            window.location.href = url;
        }, delay);
        return;
    }
    window.location.href = url;
}

/**
 * 使用 layuiTable(params) 代替
 * @param options
 * @param enable
 * @returns {*|void}
 */
function init_table_config (options, enable) {
    var configs = {
        page: true, //开启分页
        request: {
            pageName: 'page', //页码的参数名称，默认：page
            limitName: 'limit' //每页数据量的参数名，默认：limit
        },
        response: {
            statusName: 'errCode' //数据状态的字段名称，默认：code
            ,statusCode: 0 //成功的状态码，默认：0
            ,msgName: 'errMsg' //状态信息的字段名称，默认：msg
            ,countName: 'count' //数据总数的字段名称，默认：count
            ,dataName: 'data' //数据列表的字段名称，默认：data
        },
        loading: true
    };
    if (enable) {
        options.cols = options.cols.map(function (col1) {
            return col1.map(function (col) {
                if (!col.align) {
                    col.align = 'center';
                }
                return col;
            });
        });
    }
    return Object.assign(configs, options);
}

function init_table_reload(wheres) {
    return {
        where: wheres,
        page: {
            curr: 1
        }
    };
}

function init_upload_image(sel, success, params) {
    var upload_obj = {
        elem: sel, //绑定元素
        url: '/upload/image', //上传接口
        data: {
            default_key: 'img',
            prefix: 'images/',
            preview: '100x100'
        },
        field: 'img',
        done: function (data) {
            if (data.errCode == 0) {
                if (success) {
                    var resultData = data.data ? data.data : null;
                    return success(resultData);
                }
            } else {
                try {
                    top.toastr.warning(data.errMsg);
                } catch (e) {
                    console.log(data.errMsg);
                    console.log(e.message);
                }
            }
        },
        error: function (e) {
            top.toastr.error('网络请求失败，请稍后再试');
            console.log(e);
        }
    };
    return Object.assign(upload_obj, params);
}

function back($time) {
    $time = $time + '000';
    function locationBack() {
        window.history.go(-1)
    }
    setTimeout(locationBack,$time)
}


function getUediter(domID, params) {
    return UE.getEditor(domID, {
        UEDITOR_HOME_URL: 'http://' + (window.document.domain || window.location.host) + '/assets/vendors/ueditor/',
        toolbars: [
            ['source', 'undo', 'redo', '|', 'bold', 'italic', 'paragraph', 'insertorderedlist', 'insertunorderedlist','|', 'insertimage', 'removeformat', 'formatmatch'],
        ],
        initialFrameHeight: 240,
        initialFrameWidth: '100%',
        autoHeightEnabled: false
    });
}